/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */

/**
 * Encodes a tree to a single string.
 *
 * @param {TreeNode} root
 * @return {string}
 */
 var serialize = function(root) {
    if(!root){
        return '';
    }
    let res = [];
    let floor = [];
    floor.push(root);
    for(let i =0;i<floor.length;i++){
        if(floor[i]){
            floor.push(floor[i].left);
            floor.push(floor[i].right);
            res.push(floor[i].val);
        }
        else{
            res.push('null')
        }
    }
    console.log(floor,res,res.toString());
    return res.toString();
};

/**
 * Decodes your encoded data to tree.
 *
 * @param {string} data
 * @return {TreeNode}
 */
var deserialize = function(data) {
    if(!data){
        return null;
    }
    let arr = data.split(',');
    let floor = [];
    floor.push(new TreeNode(arr[0]));
    for(let i=1,j=0;i<arr.length;){
        if(floor[j]){
            let leftnode = null,rightnode=null;
            if(arr[i]!=='null'){
                leftnode = new TreeNode(arr[i]);
            }
            floor.push(leftnode);
            i++;
            if(arr[i]!=='null'){
                rightnode = new TreeNode(arr[i]);
            }
            floor.push(rightnode);
            i++;
        }
        j++;
    }
    return floor[0]
};

/**
 * Your functions will be called as such:
 * deserialize(serialize(root));
 */